function toIndex() {
    window.location.href = "/"
    selectPage(globalPageNum)
}

function login() {
    var uname = document.getElementById("adminname").value
    var pwd = document.getElementById("adminpwd").value
    console.log("aaaa")
    $.ajax({
        url: "/login/toLogin",
        method: "POST",
        data: {
            uname: uname,
            upwd: pwd
        },
        success: function (response) {
            // 处理后端响应数据
            console.log("aaaa", response)
            if (response.code === 200) {
                window.sessionStorage.setItem("username", response.user.username)
                window.sessionStorage.setItem("userid", response.user.user.uid)
                document.getElementById("beforelogin").style.display = "none";
                document.getElementById("loginok").style.display = "block";
                document.getElementById("username").value = response.user.username
                window.sessionStorage.setItem("authentication", response.authentication)
                toIndex()
            }else {
                layer.msg("账号或密码错误！！！")
            }
        }
    })
}

function selectPage(pageNum) {
    console.log("按钮被点击了！" + pageNum)
    globalPageNum = pageNum;
    window.sessionStorage.setItem("globalPageNum", pageNum)
    $.ajax({
        url: "news/getAllNews",
        method: "GET",
        data: {
            "pageNum": globalPageNum,
            "tid": globalTid,
            "keywords": globalKey
        },
        success: function (response) {
            document.getElementById("newscontent").innerHTML = response
        }
    })
}

function queryPageByTopic(tid) {
    console.log("按钮被点击了！" + tid);
    globalTid = tid
    window.sessionStorage.setItem("globalTid", tid)
    globalPageNum = 1
    $.ajax({
        url: "news/getAllNews",
        method: "GET",
        data: {
            "pageNum": globalPageNum,
            "tid": globalTid,
            "keywords": globalKey
        },
        success: function (response) {
            document.getElementById("newscontent").innerHTML = response
        }
    })
}

function search() {
    globalKey = document.getElementById("mysearch").value
    console.log("按钮被点击了！" + globalKey);
    globalPageNum = 1
    window.sessionStorage.setItem("globalKey", globalKey)
    $.ajax({
        url: "news/getAllNews",
        method: "GET",
        data: {
            "pageNum": globalPageNum,
            "tid": globalTid,
            "keywords": globalKey
        },
        success: function (response) {
            document.getElementById("newscontent").innerHTML = response
        }
    })
}

function loginOut() {
    $.ajax({
        url: "login/loginOut",
        method: "GET",
        success: function (response) {
            layer.msg("退出登陆成功！！！")
        }
    })
    window.sessionStorage.clear()
    document.getElementById("beforelogin").style.display = "block";
    document.getElementById("loginok").style.display = "none";
}

function getDetailNews(nid) {
    window.sessionStorage.setItem("nid",nid)
    $.ajax({
        url: "news/getDetailNews",
        method: "GET",
        data: {
            nid: nid
        },
        success: function (response) {
            document.getElementById("myindex").innerHTML = response
            getComment(nid)
            // document.getElementById("cauthorNoLogin")
        }
    })
}
var globalCommentPage = window.sessionStorage.getItem("globalCommentPage")
if (globalCommentPage == null) {
    globalCommentPage = 1
}
function getComment(nid) {
    $.ajax({
        url: "comment/getDetailComment",
        method: "GET",
        data: {
            nid: nid,
            pageNum:globalCommentPage
        },
        dataType: "html",
        success: function (response) {
            console.log(response)
            document.getElementById("mycomment").innerHTML = response
            // 处理后端响应数据
            // console.log(response);
        }
    })
}

function selectCommentPage(pageNum) {
    console.log("按钮被点击了！" + pageNum)
    globalCommentPage = pageNum;
    window.sessionStorage.setItem("globalCommentPage", pageNum)
    var nid = window.sessionStorage.getItem("nid")
    $.ajax({
        url: "comment/getDetailComment",
        method: "GET",
        data: {
            "pageNum": globalCommentPage,
            nid:nid
        },
        success: function (response) {
            document.getElementById("mycomment").innerHTML = response
        }
    })
}
function goBack() {
    // history.replaceState(null, "", location.href);
    window.history.go(-1)
}

function publicComment() {
    if (window.sessionStorage.getItem("username") != null) {
        var ccontent = document.getElementById("ccontent").value
        if (ccontent != null &&ccontent != '') {
            var cnid = document.getElementById("nid").value
            var cip = document.getElementById("cip").value
            var cauthor = window.sessionStorage.getItem("username")
            var authentication = window.sessionStorage.getItem("authentication")
            // if (cauthor == "这家伙很懒什么也没留下") {
            //     alert("请先登录再评论！！！")
            //     return
            // }
            $.ajax({
                url: "comment/addComment",
                method: "GET",
                data: {
                    "ccontent": ccontent,
                    "cnid": cnid,
                    "cip": cip,
                    "cauthor": cauthor
                },
                headers:{
                    authentication:authentication
                },
                // dataType: "html",
                success: function (response) {
                    //
                    // var data = JSON.parse(response)
                    // console.log("hhh" + response)
                    // console.log(data.msg)
                    // console.log(data.code)
                    if (response.code != 200) {
                        alert(response.msg)
                    } else {
                        layer.msg("评论成功！！！")
                        document.getElementById("ccontent").value = ""
                        getComment(cnid)
                    }


                }
            })
        }else {
            layer.msg("请输入内容")
            return
        }
    }else{
        alert("请先登录！！！")
    }
}
function toRegister() {
    $.ajax({
        url: "login/toRegister",
        method: "GET",
        success: function (response) {
            document.getElementById("myindex").innerHTML = response
        }
    })
}
function registerUser() {
    var formElement = document.getElementById('myRegister');
    var formData = new FormData(formElement);
    $.ajax({
        url: "login/register",
        method: "POST",
        contentType:false,
        processData: false,
        data:formData,
        success: function (response) {
            if (response.code == 200) {
                layer.msg("注册成功！！！")
                toIndex()
            }else {
                layer.msg("账号重复，请换一个！！！")
            }

        }
    })
}
